package keeto.com.vn.bms.database;

import java.util.ArrayList;

import keeto.com.vn.bms.model.BMSUserModel;
import keeto.com.vn.bms.model.NotificationModel;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

public class BMSManager {

	private BMSDBHelper helper;
	private BMSDatabase database;
	private Context context;
	private static BMSManager bmsManager;

	public BMSManager(Context context) {
		this.context = context;
		this.helper = BMSDBHelper.getInstance(context);
		this.database = BMSDatabase.getInstance(helper);
	}

	public static BMSManager getInstance(Context context){
		if(bmsManager == null){
			bmsManager = new BMSManager(context);
		}
		return bmsManager;
	}

	public boolean inputUserAccount(BMSUserModel model){
		if (database != null){
			Cursor cs = database.query(BMSDBString.USER_ACCOUNT_TABLE, null, "userID=?", new String[] {model.getUserID()}, null, null, null);

			if (cs.getCount() == 0){
				ContentValues values = new ContentValues(); 
				values.put("UserName", model.getUserName());
				values.put("FullName", model.getFullName());
				values.put("UserID", model.getUserID());
				values.put("UserEmail", model.getUserEmail());
				values.put("UserPhone", model.getUserPhone());
				values.put("UserPermission", model.getGroupId());
				database.insert(BMSDBString.USER_ACCOUNT_TABLE, null, values);
				cs.close();
				return true;
			}else{
				ContentValues values = new ContentValues(); 
				values.put("UserName", model.getUserName());
				values.put("FullName", model.getFullName());
				values.put("UserID", model.getUserID());
				values.put("UserEmail", model.getUserEmail());
				values.put("UserPhone", model.getUserPhone());
				values.put("UserPermission", model.getGroupId());
				database.update(BMSDBString.USER_ACCOUNT_TABLE, values, "userID=?", new String[] {model.getUserID()});
				cs.close();
				return true;
			}

		}
		return false;

	}

	public BMSUserModel getUserInfo(){
		BMSUserModel userModel = null;
		if (database != null){
			Cursor cs = database.rawQuery("SELECT * FROM " + BMSDBString.USER_ACCOUNT_TABLE, null);
			if (cs.getCount() == 0){
				cs.close();
			}else{
				userModel = new BMSUserModel();
				cs.moveToFirst();
				userModel.setUserName(cs.getString(cs.getColumnIndex("UserName")));
				userModel.setFullName(cs.getString(cs.getColumnIndex("FullName")));
				userModel.setUserID(cs.getString(cs.getColumnIndex("UserID")));
				userModel.setUserEmail(cs.getString(cs.getColumnIndex("UserEmail")));
				userModel.setUserPhone(cs.getString(cs.getColumnIndex("UserPhone")));
				userModel.setPermission(Integer.parseInt(cs.getString(cs.getColumnIndex("UserPermission"))));
				cs.close();
			}

		}
		return userModel;
	}

	public boolean updateNotification(NotificationModel model){
		if (database != null){
//			Cursor cs = database.rawQuery("SELECT * FROM " + BMSDBString.USER_NOTIFY_TABLE, null);
			Cursor cs = database.query(BMSDBString.USER_NOTIFY_TABLE, null, "NotifyId=?", new String[] {model.getNotifyId()}, null, null, null);
//			Cursor cursor = db.query("BuitinRoster", new String[] { "localId" },
//					"remoteId=?", new String[] { roster.getRemoterId() }, null,
//					null, null);
			if (cs.getCount() == 0){
				Log.e("SQLITE", " INPUT" + model.getNotifyId());

				try {
					ContentValues values = new ContentValues(); 
					values.put("NotifyId", model.getNotifyId());
					values.put("NotifyTitle", model.getActivitiTitle());
					values.put("NotifyContent", model.getNotifyContent());
					values.put("ObjId", model.getObjId());
					if (model.isChecked()){
						values.put("IsChecked", 1);
					}else{
						values.put("IsChecked", 0);
					}
					if(model.isRead()){
						values.put("IsReaded", 1);
					}else{
						values.put("IsReaded", 0);
					}
					values.put("UserID", model.getUserID());
					values.put("UserFullname", model.getUserFullname());
					values.put("NotifyTime", model.getNotifyTime());

					long Id =database.insert(BMSDBString.USER_NOTIFY_TABLE, null, values);

				} catch (SQLException e) {
					e.printStackTrace();
				}

			}else{
				Log.e("SQLITE", " UPDATE" + model.getNotifyId());
				ContentValues values = new ContentValues(); 
				values.put("NotifyId", model.getNotifyId());
				values.put("NotifyTitle", model.getActivitiTitle());
				values.put("NotifyContent", model.getNotifyContent());
				values.put("ObjId", model.getObjId());
				if (model.isChecked()){
					values.put("IsChecked", 1);
				}else{
					values.put("IsChecked", 0);
				}
				if(model.isRead()){
					values.put("IsReaded", 1);
				}else{
					values.put("IsReaded", 0);
				}
				values.put("UserID", model.getUserID());
				values.put("UserFullname", model.getUserFullname());
				values.put("NotifyTime", model.getNotifyTime());

				database.update(BMSDBString.USER_NOTIFY_TABLE, values, "NotifyId=?", new String[] {model.getNotifyId()});
			}
			cs.close();
			return true;
		}
		return false;
	}
	public ArrayList<NotificationModel> getListNotifycation(){
		ArrayList<NotificationModel> arrayList = new ArrayList<NotificationModel>();
		if (database != null){
			Cursor cs = database.rawQuery("SELECT * FROM " + BMSDBString.USER_NOTIFY_TABLE, null);
				for (cs.moveToFirst(); !cs.isAfterLast(); cs.moveToNext()){
					NotificationModel model = new NotificationModel();
					model.setNotifyId(cs.getString(cs.getColumnIndex("NotifyId")));
					model.setActivitiTitle(cs.getString(cs.getColumnIndex("NotifyTitle")));
					model.setNotifyContent(cs.getString(cs.getColumnIndex("NotifyContent")));
					model.setObjId(cs.getString(cs.getColumnIndex("ObjId")));
					model.setUserID(cs.getString(cs.getColumnIndex("UserID")));
					model.setUserFullname(cs.getString(cs.getColumnIndex("UserFullname")));
					model.setNotifyTime(cs.getString(cs.getColumnIndex("NotifyTime")));
					if (cs.getInt(cs.getColumnIndex("IsChecked")) == 1){
						model.setChecked(true);
					}else{
						model.setChecked(false);

					}
					if (cs.getInt(cs.getColumnIndex("IsReaded")) == 1){
						model.setChecked(true);
					}else{
						model.setChecked(false);
					}
					arrayList.add(model);
				}
			
				cs.close();

		}
		return arrayList;
	}
}
